home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 February / Macworld (1999-02).dmg / Shareware World / Shareware Feature / PageSpinner 2.0.1 / Examples / JavaScript / Cookie Example < prev    next >
Text File  |  1997-06-17  |  9KB  |  246 lines

  1. <HTML>
  2. <HEAD>
  3. <TITLE>JavaScript Cookies Demo</TITLE>
  4.  
  5. <SCRIPT>
  6. <!--
  7. /* 
  8.     Cookies Demo
  9.     By Jerry Aman, Optima System, July 28, 1996.
  10.        Cookie Functions written by Bill Dortch, hIdaho Design.
  11.  
  12.     Part of the PageSpinner distribution.
  13.  
  14.     We will not be held responsible for any unwanted 
  15.     effects due to the usage of this script or any derivative.  
  16.     No warrantees for usability for any specific application 
  17.     are given or implied.
  18.  
  19.     You are free to use and modify this script,
  20.     if credits are kept in the source code
  21. */
  22.  
  23.     var cookieVisitID     = 'my_LastVisit';    
  24.     var cookieNumVisitID     = 'my_NumVisits';    
  25.  
  26.                         // Change 'my_LastVisit' and 'my_NumVisits'
  27.                         // to something unique for
  28.                         // the current page, or else it
  29.                         // won't work correctly if other uses
  30.                         // this script!
  31.     var gLastVisit;
  32.     var gNumVisits;
  33.  
  34.     SetLastVisit();    // Execute when loading page
  35.  
  36.     function GetLastVisit ()
  37.     {
  38.  
  39.         if ( gLastVisit == "")
  40.         {
  41.             return "Welcome to this site!";
  42.         }
  43.         else 
  44.         {
  45.             var oldVisitDate = new Date(gLastVisit);
  46.             return     "Welcome back, your last visit to this site was on " 
  47.                     + gLastVisit +".<BR>You have been here " 
  48.                     + gNumVisits + " time" +(gNumVisits>1 ? "s" :"") 
  49.                     +" before."
  50.         }
  51.     }
  52.  
  53.  
  54.     function SetLastVisit (name, value) 
  55.     {
  56.         var newVisitDate = new Date();
  57.         var expDate = new Date (); 
  58.         var numVisits = 0;
  59.  
  60.             // The expDate is the date when the cookie should
  61.             // expire, we will keep it for a year
  62.         expDate.setTime( expDate.getTime() + (365 * 24 * 60 * 60 * 1000) ); 
  63.  
  64.             // Info about last visit
  65.         if (GetCookie (cookieVisitID) != null)
  66.             gLastVisit = GetCookie (cookieVisitID);
  67.         else
  68.             gLastVisit = "";
  69.  
  70.         if (GetCookie (cookieNumVisitID) != null)
  71.             gNumVisits = GetCookie (cookieNumVisitID);    
  72.         else
  73.             gNumVisits = 0;
  74.  
  75.             // Use eval() to convert a string to a number
  76.         numVisits = eval(gNumVisits) +1;    
  77.  
  78.             // Store info about this visit
  79.         SetCookie( cookieVisitID,     newVisitDate, expDate); 
  80.         SetCookie( cookieNumVisitID, numVisits, expDate); 
  81.     }
  82.  
  83.     // ---------------------------------------------------------------
  84.     //  Cookie Functions - Second Helping  (21-Jan-96)
  85.     //  Written by:  Bill Dortch, hIdaho Design <BDORTCH@NETW.COM>
  86.     //  The following functions are released to the public domain.
  87.     //
  88.     //  The Second Helping version of the cookie functions dispenses with
  89.     //  my encode and decode functions, in favor of JavaScript's new built-in
  90.     //  escape and unescape functions, which do more complete encoding, and
  91.     //  which are probably much faster.
  92.     //
  93.     //  The new version also extends the SetCookie function, though in
  94.     //  a backward-compatible manner, so if you used the First Helping of
  95.     //  cookie functions as they were written, you will not need to change any
  96.     //  code, unless you want to take advantage of the new capabilities.
  97.     //
  98.     //  The following changes were made to SetCookie:
  99.     //
  100.     //  1.  The expires parameter is now optional - that is, you can omit
  101.     //      it instead of passing it null to expire the cookie at the end
  102.     //      of the current session.
  103.     //
  104.     //  2.  An optional path parameter has been added.
  105.     //
  106.     //  3.  An optional domain parameter has been added.
  107.     //
  108.     //  4.  An optional secure parameter has been added.
  109.     //
  110.     //  For information on the significance of these parameters, and
  111.     //  and on cookies in general, please refer to the official cookie
  112.     //  spec, at:
  113.     //
  114.     //      http://www.netscape.com/newsref/std/cookie_spec.html    
  115.     //
  116.     //
  117.     // "Internal" function to return the decoded value of a cookie
  118.     //
  119.     function getCookieVal (offset) {
  120.       var endstr = document.cookie.indexOf (";", offset);
  121.       if (endstr == -1)
  122.         endstr = document.cookie.length;
  123.       return unescape(document.cookie.substring(offset, endstr));
  124.     }
  125.  
  126.     //
  127.     //  Function to return the value of the cookie specified by "name".
  128.     //    name - String object containing the cookie name.
  129.     //    returns - String object containing the cookie value, or null if
  130.     //      the cookie does not exist.
  131.     //
  132.     function GetCookie (name) {
  133.       var arg = name + "=";
  134.       var alen = arg.length;
  135.       var clen = document.cookie.length;
  136.       var i = 0;
  137.       while (i < clen) {
  138.         var j = i + alen;
  139.         if (document.cookie.substring(i, j) == arg)
  140.           return getCookieVal (j);
  141.         i = document.cookie.indexOf(" ", i) + 1;
  142.         if (i == 0) break; 
  143.       }
  144.       return null;
  145.     }
  146.  
  147.     //
  148.     //  Function to create or update a cookie.
  149.     //    name - String object object containing the cookie name.
  150.     //    value - String object containing the cookie value.  May contain
  151.     //      any valid string characters.
  152.     //    [expires] - Date object containing the expiration data of the cookie.  If
  153.     //      omitted or null, expires the cookie at the end of the current session.
  154.     //    [path] - String object indicating the path for which the cookie is valid.
  155.     //      If omitted or null, uses the path of the calling document.
  156.     //    [domain] - String object indicating the domain for which the cookie is
  157.     //      valid.  If omitted or null, uses the domain of the calling document.
  158.     //    [secure] - Boolean (true/false) value indicating whether cookie transmission
  159.     //      requires a secure channel (HTTPS).  
  160.     //
  161.     //  The first two parameters are required.  The others, if supplied, must
  162.     //  be passed in the order listed above.  To omit an unused optional field,
  163.     //  use null as a place holder.  For example, to call SetCookie using name,
  164.     //  value and path, you would code:
  165.     //
  166.     //      SetCookie ("myCookieName", "myCookieValue", null, "/");
  167.     //
  168.     //  Note that trailing omitted parameters do not require a placeholder.
  169.     //
  170.     //  To set a secure cookie for path "/myPath", that expires after the
  171.     //  current session, you might code:
  172.     //
  173.     //      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
  174.     //
  175.     function SetCookie (name, value) {
  176.       var argv = SetCookie.arguments;
  177.       var argc = SetCookie.arguments.length;
  178.       var expires = (argc > 2) ? argv[2] : null;
  179.       var path = (argc > 3) ? argv[3] : null;
  180.       var domain = (argc > 4) ? argv[4] : null;
  181.       var secure = (argc > 5) ? argv[5] : false;
  182.       document.cookie = name + "=" + escape (value) +
  183.         ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
  184.         ((path == null) ? "" : ("; path=" + path)) +
  185.         ((domain == null) ? "" : ("; domain=" + domain)) +
  186.         ((secure == true) ? "; secure" : "");
  187.     }
  188.  
  189.     //  Function to delete a cookie. (Sets expiration date to current date/time)
  190.     //    name - String object containing the cookie name
  191.     //
  192.     function DeleteCookie (name) {
  193.       var exp = new Date();
  194.       exp.setTime (exp.getTime() - 1);  // This cookie is history
  195.       var cval = GetCookie (name);
  196.       document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
  197.     }
  198.  
  199. // -->
  200. </SCRIPT>
  201.  
  202. </HEAD>
  203.  
  204. <BODY BGCOLOR=FFFFFF TEXT=000000>
  205.  
  206. <H1>JavaScript Cookies Example</H1>
  207.  
  208. <B>This page contains a JavaScript that stores information about the last time a reader visited the page</B>
  209. <P>
  210. Please note that JavaScript is currently only available in Netscape Navigator 2.0 or higher.<BR>
  211. <FONT COLOR="931B15">Do not assume that all in your audience are using a JavaScript enabled browser.</FONT>
  212. <HR>
  213. <P>
  214. These JavaScripts use Persistent Client State HTTP Cookies to store information about the last time a reader visited the page. A cookie is a mechanism that allows information to be stored across multiple sessions.
  215. <P>
  216. In this example two cookies are stored containing information about last date and number of times the reader has visited a page (containing these cookies). Since the cookies are stored on the readers hard disk you - the author - will not be able to extract information about the reader.
  217. <P>
  218. Cookies can also be used for many other purposes, such as keeping readers preferences for viewing a site, storing passwords, implementing a shopping cart for online business or for keeping track of access rights for a specific reader viewing pages created on the fly on the server.
  219. <HR>
  220. <B>Here is an example:</B>
  221.  
  222. <P>
  223. Hello, <SCRIPT>document.write(navigator.userAgent);</SCRIPT>!
  224.  
  225. <P>
  226. <SCRIPT>document.write (GetLastVisit())</SCRIPT>
  227.  
  228. <P>
  229. Reload this page to see the text above updated.
  230.  
  231. <P>
  232. <HR>
  233. <B>How to use:</B><BR>
  234. Rename the constants <CODE>'my_LastVisit'</CODE> and '<CODE>my_NumVisits'</CODE> to something unique for the current page (maybe use some part ot the url) or else it won't work correctly if other uses this script! These constants are located at the beginning of the HEAD section in this file. It is important to do this!
  235.  
  236. <P>
  237. You may want to change the text inside function <CODE>GetLastVisit()</CODE> to something more appropriate for your  site.
  238.  
  239. <P>
  240. Finally use this code inside the BODY section to display the information in the browser:
  241. <XMP><SCRIPT>document.write (GetLastVisit())</SCRIPT></XMP>
  242.  
  243. <P>
  244. </BODY>
  245. </HTML>
  246.